package com.spring.theory.bean.lifeCycle;

import static com.spring.theory.constant.GeneralConstant.LOG_INFO;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;


/**
 * 阅读 2-1-2
 * bean的后处理器方法：对bean生命周期的扩展
 * beanFactory的后处理器方法：对bean定的扩展
 * <p>
 * 使用了模板方法； - bean的生命周期是固定的，但是对于每个生命周期阶段是可以做增强；每一种增强的方法都抽象在接口里面，方便扩展。
 *
 * @author 24373
 * @date 2023/12/24 11:26
 */
@Component
@Slf4j
public class OriginalBeanPostProcessor implements InstantiationAwareBeanPostProcessor, DestructionAwareBeanPostProcessor {

    private static boolean specialBean(String beanName) {
        return "lifeCycleBean".equals(beanName);
    }

    /**
     * BeanPostProcessor InstantiationAwareBeanPostProcessor--实例化前
     */
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if (specialBean(beanName)) {
            log.info(LOG_INFO + "InstantiationAwareBeanPostProcessor实例化前执行，这里返回的bean会替换原本bean；返回null不替换");
        }
        return null;
    }

    /**
     * BeanPostProcessor InstantiationAwareBeanPostProcessor--实例化后
     */
    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if (specialBean(beanName)) {
            log.info(LOG_INFO + "InstantiationAwareBeanPostProcessor--实例化后执行，如果返回false会跳过依赖注入阶段");
            //return false;
        }
        return true;
    }

    /**
     * BeanPostProcessor InstantiationAwareBeanPostProcessor--依赖注入阶段
     */
    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        if (specialBean(beanName)) {
            log.info(LOG_INFO + "InstantiationAwareBeanPostProcessor--bean依赖注入阶段，如@AutoWire，@Value，@Resource");
        }
        return pvs;
    }

    /**
     * BeanPostProcessor InstantiationAwareBeanPostProcessor--初始化前
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (specialBean(beanName)) {
            log.info(LOG_INFO + "BeanPostProcessor--bean初始化之前执行，这里返回的bean会替换原本bean；如@PostConstruct，@ConfigurationProperties");
        }
        return bean;
    }

    /**
     * BeanPostProcessor InstantiationAwareBeanPostProcessor--初始化后
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (specialBean(beanName)) {
            log.info(LOG_INFO + "BeanPostProcessor--bean初始化之后执行，这里返回的bean会替换原本bean；如代理增强");
        }
        return bean;
    }

    /**
     * BeanPostProcessor DestructionAwareBeanPostProcessor--销毁前
     */
    @Override
    public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
        if (specialBean(beanName)) {
            log.info(LOG_INFO + "DestructionAwareBeanPostProcessor--bean销毁前执行，如@PreDestory");
        }
    }

}
